﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ajayumi.Platform.Search.Win
{
    public partial class SearchRangeTextBox : ajayumi.Platform.Search.Win.SearchControlBase
    {

        public override string DisplayText
        {
            set
            {
                this.label1.Text = value;
            }
        }

        public SearchRangeTextBox()
        {
            InitializeComponent();
        }

        internal override SearchItemValue GetSearchItemValue()
        {
            return new SearchItemValue()
            {
                ItemIndex = this.ItemIndex,
                Value1 = this.textBox1.Text.Trim(),
                Value2 = this.textBox2.Text.Trim()
            };
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            this.ValidateNumber((TextBox)sender, e);
        }

        private void textBox2_Validating(object sender, CancelEventArgs e)
        {
            this.ValidateNumber((TextBox)sender, e);
        }

        private  void ValidateNumber(TextBox control,  CancelEventArgs e)
        {
            string text = control.Text.Trim();
            if (!string.IsNullOrEmpty(text) && this.ValueType == Search.ValueType.Number)
            {
                bool result = Regex.IsMatch(text, @"^[-]?\d+[.]?\d*$");
                if (result)
                {
                    control.Margin = new Padding(3);
                    this.errorProvider1.SetError(control, string.Empty);
                }
                else
                {
                    control.SelectAll();
                    control.Margin = new Padding(control.Margin.Left, control.Margin.Top, 18, control.Margin.Bottom);
                    this.errorProvider1.SetError(control, "请输入数值");
                    e.Cancel = true;
                }
            }
        }

    }
}
